<template>
  <div class="reading-power">
    <div>
      <fieldset>
        <legend>上月抄表底数(千瓦<sub>.</sub>时):</legend>
        <div>
          <el-table :data="lastMonthTab" style="width: 100%" height="190">
            <el-table-column prop="label" label="" width="100" />
            <el-table-column prop="value" label="值" width="140" />
          </el-table>
        </div>
      </fieldset>
      <fieldset>
        <legend>互感器:</legend>
        <div>
          <el-table :data="transformers" style="width: 100%" height="160">
            <el-table-column prop="prop" label="" width="100" />
            <el-table-column prop="values" label="值" width="180" />
          </el-table>
        </div>
      </fieldset>
    </div>
    <div>
      <fieldset>
        <legend>本月抄表底数(千瓦.时):</legend>
        <div>
          <el-table :data="monthTab" style="width: 100%" height="190">
            <el-table-column prop="label" label="" width="180" />
            <el-table-column prop="value" label="值" width="180" />
          </el-table>
        </div>
      </fieldset>
      <fieldset>
        <legend>抄见底数(千瓦.时):</legend>
        <div>
          <el-table :data="sumMonthTab" style="width: 100%" height="160">
            <el-table-column prop="label" label="" width="180" />
            <el-table-column prop="value" label="值" width="180" />
          </el-table>
        </div>
      </fieldset>
    </div>
  </div>
</template>
<script setup>
import { ref, reactive, onMounted } from "vue";
import { useAppStore } from "@/stores";
import { storeToRefs } from "pinia";
const useApp = useAppStore();
let { monthMeterReading, organStructure } = storeToRefs(useApp);

import {
  transformerData,
  kvaData,
  voltageLevelData,
  powerFactorData,
} from "@/gm/comm.js";
import * as math from "mathjs";
const props = defineProps({
  position: String,
});
const ruleForm = reactive({
  eleType: "",
  transformer: "",
  kva: "",
  voltageLevel: "",
  powerFactor: "",
  accountingPlan: "",
});
const rules = ref([]);
const transformers = ref([
  {
    prop: "电压变化",
    values: "1V/1V",
  },
  {
    prop: "电流变化",
    values: "50A/5A",
  },
  {
    prop: "倍率",
    values: "10",
  },
]);
const ruleFormRef = ref();
const eleTypeVisible = ref(false);
const tableData = [
  {
    id: 1,
    value: "11",
    prop1: "byzxygfl1", //当前
    prop2: "syzxygfl1", //上月
    label: "正向有功尖",
  },
  {
    id: 2,
    value: "22",
    prop1: "byzxygfl2",
    prop2: "syzxygfl2",
    label: "正向有功峰",
  },
  {
    id: 3,
    value: "33",
    prop1: "byzxygfl3",
    prop2: "syzxygfl3",
    label: "正向有功平",
  },
  {
    id: 4,
    value: "44",
    prop1: "byzxygfl4",
    prop2: "syzxygfl4",
    label: "正向有功谷",
  },
  {
    id: 5,
    value: "44",
    prop1: "byzxygz",
    prop2: "syzxygfl",
    label: "正向有功总",
  },
  {
    id: 6,
    value: "11",
    prop1: "byfxygfl1",
    prop2: "syfxygjdn",
    label: "反向有功尖",
  },
  {
    id: 7,
    value: "22",
    prop1: "byfxygfl2",
    prop2: "syfxygfdn",
    label: "反向有功峰",
  },
  {
    id: 8,
    value: "33",
    prop1: "byfxygfl3",
    prop2: "syfxygpdn",
    label: "反向有功平",
  },
  {
    id: 9,
    value: "44",
    prop1: "byfxygfl4",
    prop2: "syfxyggdn",
    label: "反向有功谷",
  },
  {
    id: 10,
    value: "44",
    prop1: "byfxygfl",
    prop2: "syfxygzdn",
    label: "反向有功总",
  },
  { id: 11, value: "44", prop1: "bywgz", prop2: "sywgz", label: "无功总" },
];
const lastMonthTab = ref([]);
const monthTab = ref([]);
const sumMonthTab = ref([]);

const showEleType = () => {
  eleTypeVisible.value = true;
};
const isShow = () => {
  eleTypeVisible.value = false;
};
onMounted(() => {
  lastMonthTab.value = JSON.parse(JSON.stringify(tableData));
  monthTab.value = JSON.parse(JSON.stringify(tableData));
  sumMonthTab.value = JSON.parse(JSON.stringify(tableData));
  if (!isEmpty(monthMeterReading.value)) {
    if (monthMeterReading.value[props.position]) initData();
  }
  getSumMonthTab(); //本月抄表数据减去上月抄表数据
  regHGQData(organStructure.value[0].children); // 填写互感器数据
});
//学生界面抄表数据赋值
const initData = () => {
  monthMeterReading.value[props.position].forEach((item, index) => {
    //判断上月还是本月
    if (item.parm2) {
      lastMonthTab.value.forEach((item1, index1) => {
        if (item1.prop2 === item.prop) {
          item1.value = item["value" + props.position];
        }
      });
    } else {
      monthTab.value.forEach((item1, index1) => {
        if (item1.prop1 === item.prop) {
          item1.value = item["value" + props.position];
        }
      });
    }
  });
};
//本月抄表数据减去上月抄表数据
const getSumMonthTab = () => {
  sumMonthTab.value.forEach((item, index) => {
    item.value =
      Number(monthTab.value[index].value) -
      Number(lastMonthTab.value[index].value);
  });
};
const isEmpty = (obj) => {
  return Object.keys(obj).length === 0;
};
// 填写互感器数据
const regHGQData = (data) => {
  data.forEach((item) => {
    if (item.position === Number(props.position)) {
      let bl = "";
      if (item.ptRatio != "---") {
        bl = math.evaluate(item.ptRatio.replace(/[kV]/, ""));
      }
      // if (item.ptRatio === "10kV/0.4kV") bl = "250";
      transformers.value[0].values = item.ptRatio;
      transformers.value[1].values = item.ctRatio;
      transformers.value[2].values = bl;
    }
    if (item.children) {
      regHGQData(item.children);
    }
  });
};
</script>

<style lang="scss" scoped>
.reading-power {
  display: flex;
  // height: 200px;
  overflow: auto;
  .ruleForm {
    .el-form-item {
      margin-bottom: 15px;
    }
    .el-input {
      width: 32%;
    }
    .el-button {
      margin-left: 10px;
      width: 80px;
    }
  }
}
</style>
